//
//  HMHomeRefresh.m
//  weibo
//
//  Created by apple on 2016/11/20.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "HMHomeRefresh.h"

@interface HMHomeRefresh ()
@property(nonatomic,strong)UILabel *lb_text;
@property(nonatomic,strong)UIImageView *iv_img;
@property(nonatomic,assign) HmrefState refState;
@property(nonatomic,strong) UIScrollView *scrollview;
@property(nonatomic,strong) UIActivityIndicatorView *dicatorView;
@property(nonatomic,strong) UIImageView *iv_backView;
@property(nonatomic,assign)BOOL isani;
@end

@implementation HMHomeRefresh
-(UIImageView *)iv_backView{
    if (!_iv_backView) {
        _iv_backView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"refreshbg"]];
//        _iv_backView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _iv_backView;
}
-(UIActivityIndicatorView *)dicatorView{
    if (!_dicatorView) {
        _dicatorView = [[UIActivityIndicatorView alloc]init];
    }
    return _dicatorView;
}
-(UILabel *)lb_text{
    if (!_lb_text) {
        _lb_text = [[UILabel alloc]initWithText:@"willRefresh" textColor:[UIColor grayColor] textAlignment:NSTextAlignmentCenter fontSize:15];
    }
    return _lb_text;
}
-(UIImageView *)iv_img{
    if (!_iv_img) {
        _iv_img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"tableview_pull_refresh"]];
    }
    return _iv_img;
}
-(void)setRefState:(HmrefState)refState{
    _refState = refState;
    switch (refState) {
        case Normal:
            self.iv_img.hidden = NO;
            [self changeTheDirect:refState];
            self.lb_text.text = @"下拉刷新";
            break;
        case Alerady:
            [self changeTheDirect:refState];
            self.lb_text.text = @"准备刷新";
            break;
        case Refreshing:
            self.iv_img.hidden = YES;
            [self.dicatorView startAnimating];
            [self changeTheDirect:refState];
            self.lb_text.text = @"正在刷新";
            break;
        default:
            break;
    }
}
-(void)changeTheDirect:(HmrefState)state{
    if (state == Normal) {
        [UIView animateWithDuration:0.25 animations:^{
            self.iv_img.transform = CGAffineTransformIdentity;
        }];
        if (self.isani) {
            self.scrollview.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
            self.isani = NO;
        }
 
    }else if (state == Alerady){
    [UIView animateWithDuration:0.25 animations:^{
        self.iv_img.transform = CGAffineTransformMakeRotation(M_PI);

    }];
    }else{
    [UIView animateWithDuration:0.25 animations:^{
        self.isani = YES;
        CGFloat offset = 64 + self.scrollview.contentInset.top;
        self.scrollview.contentInset = UIEdgeInsetsMake(offset, 0, 0, 0);
//        self.scrollview.contentInset.top = self.scrollview.contentInset.top + 64;
    }];
    }
}

-(void)endHMHomeRefeshAnimation{
    [self.dicatorView stopAnimating];
    self.refState = Normal;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}
//- (instancetype)init
//{
//    self = [super init];
//    if (self) {
//        [self setupUI];
//    }
//    return self;
//}
-(void)setupUI{
//    self.backgroundColor = [UIColor redColor];
    [self addSubview:self.iv_backView];
    [self addSubview:self.iv_img];
    [self addSubview:self.lb_text];
    [self addSubview:self.dicatorView];
    [_iv_backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.bottom.trailing.equalTo(self);
    }];
    [_lb_text mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.mas_centerX).offset(5);
        make.centerY.equalTo(self);
    }];
    [_iv_img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.mas_centerX).offset(-10);
        make.centerY.equalTo(self);
    }];
    [_dicatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(_iv_img);
    }];
    [self layoutIfNeeded];
}

-(void)willMoveToSuperview:(UIView *)newSuperview{
    [super willMoveToSuperview:newSuperview];
    if ([newSuperview isKindOfClass:[UIScrollView class]]) {
        _scrollview = (UIScrollView*)newSuperview;

        [_scrollview addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
    }
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    CGFloat offset = _scrollview.contentOffset.y;
    CGFloat targetOffset = -(self.scrollview.contentInset.top + 44);
  
    if (_scrollview.isDragging) {
        if (self.refState==Normal && offset<targetOffset) {
            self.refState=Alerady;
            NSLog(@"Alerady");
        }else if (self.refState==Alerady && offset>targetOffset){
            self.refState = Normal;
            NSLog(@"Normal");
        }
    }else{
        if (self.refState==Alerady && offset<(targetOffset+20)) {
            self.refState = Refreshing;
//            NSLog(@"%.2f",offset);
            NSLog(@"refresh");
            [self sendActionsForControlEvents:UIControlEventValueChanged];
            [self.dicatorView startAnimating];
        }
        
    }
    
}

//-(void)addObserTosuperViewcontroll{
//    UIScrollView *view = [];
//
//}



@end
